package com.blue.common.entity;

import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * 通用的结果返回类
 *
 * @param <T>
 */
@Data
public class ResponseResult<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;
    private String traceId;

    public ResponseResult() {
        this.code = 200;
        this.traceId = MDC.get("traceId");
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
        this.traceId = MDC.get("traceId");
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.traceId = MDC.get("traceId");
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.traceId = MDC.get("traceId");
    }

    public static <T> ResponseResult<T> errorResult(String msg) {
        ResponseResult<T> result = new ResponseResult<>();
        return result.error(500, msg);
    }

    public static <T> ResponseResult<T> errorResult(Integer code, String msg) {
        ResponseResult<T> result = new ResponseResult<>();
        return result.error(code, msg);
    }

    public static <T> ResponseResult<T> errorResult() {
        ResponseResult<T> result = new ResponseResult<>();
        return result.error(500, "操作失败");
    }

    public static <T> ResponseResult<T> okResult() {
        ResponseResult<T> result = new ResponseResult<>();
        return result.ok(200, null, "操作成功");
    }

    public static <T> ResponseResult<?> okResult(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        return result.ok(200, data, "操作成功");
    }

    public ResponseResult<T> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResponseResult<T> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(T data) {
        this.data = data;
        return this;
    }
}

